home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / fastobj.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  202 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    fastobj -
  19.  *        support for spin format objects. This is obsoleted
  20.  *    by sgiobj files.
  21.  *
  22.  *                Paul Haeberli - 1989
  23.  */
  24. #include "stdio.h"
  25. #include "gl.h"
  26. #include "device.h"
  27. #include "spin.h"
  28. #include "resource.h"
  29.  
  30. #define POLYGON    0
  31. #define LINES    1
  32.  
  33. int lflag;
  34.  
  35. #define PolyOrLine()    if (lflag == LINES) {         \
  36.                 bgnclosedline();     \
  37.             } else {             \
  38.                 bgnpolygon();         \
  39.             }
  40.  
  41. #define EndPolyOrLine() if (lflag == LINES) {         \
  42.                 endclosedline();     \
  43.             } else {             \
  44.                 endpolygon();         \
  45.             }
  46.  
  47. fastobjlines(v)
  48. int v;
  49. {
  50.     if(v)
  51.     lflag = LINES;
  52.     else
  53.     lflag = POLYGON;
  54. }
  55.  
  56. fastobj *readfastobj(name)
  57. char *name;
  58. {
  59.     FILE *inf;
  60.     fastobj *obj;
  61.     int nlongs;
  62.     int magic;
  63.     char filename[512];
  64.  
  65.     inf = res_fopen(name,"r");
  66.     if(!inf) {
  67.         fprintf(stderr,"readfastobj: can't open input file %s\n",name);
  68.         exit(1);
  69.     }
  70.     res_fread(&magic,sizeof(int),1,inf);
  71.     if(magic != FASTMAGIC) {
  72.     fprintf(stderr,"readfastobj: bad magic %d in object file\n",magic);
  73.     fclose(inf);
  74.         exit(1);
  75.     }
  76.     obj = (fastobj *)mymalloc(sizeof(fastobj));
  77.     res_fread(&obj->npoints,sizeof(int),1,inf);
  78.     res_fread(&obj->colors,sizeof(int),1,inf);
  79.     nlongs = 6*obj->npoints;
  80.     obj->data = (int *)mymalloc(nlongs*sizeof(int));
  81.     res_fread(obj->data,nlongs*sizeof(int),1,inf);
  82.     res_fclose(inf);
  83.     return obj;
  84. }
  85.  
  86. writefastobj(name,obj)
  87. char *name;
  88. fastobj *obj;
  89. {
  90.     FILE *outf;
  91.     int nlongs;
  92.     int magic;
  93.     char filename[512];
  94.  
  95.     outf = fopen(name,"w");
  96.     if(!outf) {
  97.         fprintf(stderr,"writefastobj: can't open input file %s\n",name);
  98.         exit(1);
  99.     }
  100.     magic = FASTMAGIC;
  101.     fwrite(&magic,sizeof(int),1,outf);
  102.     fwrite(&obj->npoints,sizeof(int),1,outf);
  103.     fwrite(&obj->colors,sizeof(int),1,outf);
  104.     nlongs = 6*obj->npoints;
  105.     fwrite(obj->data,nlongs*sizeof(int),1,outf);
  106.     fclose(outf);
  107. }
  108.  
  109. fastobj *clonefastobj(obj)
  110. fastobj *obj;
  111. {
  112.     fastobj *cobj;
  113.     int nlongs;
  114.  
  115.     cobj = (fastobj *)mymalloc(sizeof(fastobj));
  116.     *cobj = *obj;
  117.     nlongs = 6*obj->npoints;
  118.     cobj->data = (int *)mymalloc(nlongs*sizeof(int));
  119.     bcopy(obj->data,cobj->data,nlongs*sizeof(int));
  120.     return cobj;
  121. }
  122.  
  123. fastobj *newfastobj(npolys)
  124. int npolys;
  125. {
  126.     fastobj *obj;
  127.     int nlongs;
  128.  
  129.     obj = (fastobj *)mymalloc(sizeof(fastobj));
  130.     obj->colors = 0;
  131.     obj->npoints = 4*npolys;
  132.     nlongs = 6*obj->npoints;
  133.     obj->data = (int *)mymalloc(nlongs*sizeof(int));
  134.     return obj;
  135. }
  136.  
  137. drawfastobj(obj)
  138. fastobj *obj;
  139. {
  140.     register long *p, *end;
  141.     register int npolys;
  142.  
  143.     p = (long *)obj->data;
  144.     end = (long *)(p + 6*obj->npoints);
  145.     if(obj->colors) {
  146.     npolys = obj->npoints/4;
  147.     while(npolys--) {
  148.         PolyOrLine();
  149.         c3i(p);
  150.         v3f((float*)p+3);
  151.         p += 6;
  152.         c3i(p);
  153.         v3f((float*)p+3);
  154.         p += 6;
  155.         c3i(p);
  156.         v3f((float*)p+3);
  157.         p += 6;
  158.         c3i(p);
  159.         v3f((float*)p+3);
  160.         p += 6;
  161.         EndPolyOrLine();
  162.     }
  163.     } else {
  164.     while ( p < end) {
  165.         PolyOrLine();
  166.         n3f((float*)p);
  167.         v3f((float*)p+3);
  168.         n3f((float*)p+6);
  169.         v3f((float*)p+9);
  170.         n3f((float*)p+12);
  171.         v3f((float*)p+15);
  172.         n3f((float*)p+18);
  173.         v3f((float*)p+21);
  174.         EndPolyOrLine();
  175.         p+=24;
  176.     }
  177.     }
  178. }
  179.  
  180. drawflatobj(obj)
  181. fastobj *obj;
  182. {
  183.     register int *p,*end;
  184.     register int npolys;
  185.  
  186.     p = obj->data;
  187.     end = p + 6*obj->npoints;
  188.     while ( p < end) {
  189.     bgnpolygon();
  190.     v3f((float*)p+3);
  191.     v3f((float*)p+9);
  192.     v3f((float*)p+15);
  193.     endpolygon();
  194.     bgnpolygon();
  195.     v3f((float*)p+15);
  196.     v3f((float*)p+21);
  197.     v3f((float*)p+3);
  198.     endpolygon();
  199.     p+=24;
  200.     }
  201. }
  202.